package com.nc.delivery_project.client.order_details;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.ListDataProvider;
import com.nc.delivery_project.client.cargo_details.CargoDetailsPlace;
import com.nc.delivery_project.shared.proxy.CargoProxy;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 1/26/15
 * Time: 9:44 PM
 */
public class OrderDetailsView extends Composite {

    interface OrderDetailsViewUiBinder extends UiBinder<VerticalPanel, OrderDetailsView> {
    }

    private ListDataProvider<CargoProxy> dataProvider;

    private List<CargoProxy> dataList;

    ColumnSortEvent.ListHandler<CargoProxy> sortHandler;
    private static OrderDetailsViewUiBinder uiBinder = GWT.create(OrderDetailsViewUiBinder.class);

    @UiField
    VerticalPanel panel;

    @UiField
    TextBox filterTxtBox;

    @UiField
    Label orderIdLbl;

    @UiField
    Label senderLbl;

    @UiField
    Label receiverLbl;

    @UiField
    TextBox startDateTxtBox;

    @UiField
    TextBox deliveryDateTxtBox;

    @UiField
    TextBox commentTxtBox;

    @UiField
    TextBox statusTxtBox;

    @UiField
    Label paymentLbl;

    @UiField
    DataGrid<CargoProxy> cargoesDataGrid;

    @UiField
    SimplePager pager;

    @UiField
    Button addBtn;

    @UiField
    Button saveOBtn;

    @UiField
    Button removeOBtn;




    private OrderDetailsActivity presenter;

    public OrderDetailsView() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    public void setPresenter(OrderDetailsActivity presenter) {
        this.presenter = presenter;
    }
    public void start() {
        panel.setCellWidth(cargoesDataGrid, "100%");
        panel.setCellWidth(pager, "100%");

        cargoesDataGrid.setWidth("50%");
        cargoesDataGrid.setHeight("100px");
        cargoesDataGrid.setEmptyTableWidget(new HTML("No Cargoes"));

        pager.setDisplay(cargoesDataGrid);

        dataProvider = new ListDataProvider<>();
        dataProvider.setList(new ArrayList<CargoProxy>());

        sortHandler = new ColumnSortEvent.ListHandler<>(dataProvider.getList());

        initTableColumns(cargoesDataGrid, sortHandler);

        cargoesDataGrid.addColumnSortHandler(sortHandler);

        dataProvider.addDataDisplay(cargoesDataGrid);

        filterTxtBox.addKeyUpHandler(new KeyUpHandler() {
            @Override
            public void onKeyUp(KeyUpEvent event) {
                filter(filterTxtBox.getText());
            }
        });

        addBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                //  presenter.goTo(new AddCargoPlace(""));
            }
        });
    }
    public void initTableColumns(DataGrid<CargoProxy> dataGrid, ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> idCol = createIdCol(sortHandler);
        Column<CargoProxy, String> titleCol = createTitleCol(sortHandler);
        Column<CargoProxy, String> costCol = createCostCol(sortHandler);
        Column<CargoProxy, String> currencyCol = createCurrencyCol(sortHandler);
        Column<CargoProxy, String> widthCol = createWidthCol(sortHandler);
        Column<CargoProxy, String> heightCol = createHeightCol(sortHandler);
        Column<CargoProxy, String> weightCol = createWeightCol(sortHandler);
        Column<CargoProxy, String> depthCol = createDepthCol(sortHandler);

//        Column<CargoProxy, String> addCargoBtnCol = addCargoBtnCol();
        Column<CargoProxy, String> editCargoBtnCol=editCargoBtnCol();

        dataGrid.addColumn(idCol, "ID");
        dataGrid.addColumn(titleCol, "Title");
        dataGrid.addColumn(costCol, "Cost");
        dataGrid.addColumn(currencyCol, "Currency");
        dataGrid.addColumn(widthCol, "Width");
        dataGrid.addColumn(heightCol, "Height");
        dataGrid.addColumn(weightCol, "Weight");
        dataGrid.addColumn(depthCol, "Depth");
        dataGrid.addColumn(editCargoBtnCol, "Edit");
    }

    private Column<CargoProxy, String> createDepthCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return String.valueOf(object.getDepth());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return String.valueOf(o1.getDepth()).compareToIgnoreCase(String.valueOf(o2.getDepth()));
            }
        });
        return col;
    }

    private Column<CargoProxy, String> createWeightCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return String.valueOf(object.getWeight());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return String.valueOf(o1.getWeight()).compareToIgnoreCase(String.valueOf(o2.getWeight()));
            }
        });
        return col;
    }

    private Column<CargoProxy, String> createHeightCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return String.valueOf(object.getHeight());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return String.valueOf(o1.getHeight()).compareToIgnoreCase(String.valueOf(o2.getHeight()));
            }
        });
        return col;
    }

    private Column<CargoProxy, String> createWidthCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return String.valueOf(object.getWidth());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return String.valueOf(o1.getWidth()).compareToIgnoreCase(String.valueOf(o2.getWidth()));
            }
        });
        return col;
    }

    private Column<CargoProxy, String> createCurrencyCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return object.getCurrency().getTitle();
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return o1.getCurrency().getTitle().compareToIgnoreCase(o2.getCurrency().getTitle());
            }
        });
        return col;
    }

    private Column<CargoProxy, String> createCostCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return String.valueOf(object.getCost());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return String.valueOf(o1.getCost()).compareToIgnoreCase(String.valueOf(o2.getCost()));
            }
        });
        return col;
    }

    private Column<CargoProxy, String> createTitleCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler) {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return object.getTitle();
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                return o1.getTitle().compareToIgnoreCase(o2.getTitle());
            }
        });
        return col;
    }
    private Column<CargoProxy, String> createIdCol(ColumnSortEvent.ListHandler<CargoProxy> sortHandler)
    {
        Column<CargoProxy, String> col = new Column<CargoProxy, String>(new TextCell())
        {
            @Override
            public String getValue(CargoProxy object)
            {
                return String.valueOf(object.getId());
            }
        };

        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<CargoProxy>() {
            @Override
            public int compare(CargoProxy o1, CargoProxy o2) {
                if (o1.getId() == o2.getId()) {
                    return 0;
                } else {
                    return o1.getId() > o2.getId() ? 1 : -1;
                }
            }
        });
        return col;
    }

    private Column<CargoProxy, String> editCargoBtnCol() {
        Column<CargoProxy, String> editCargoBtnCol = new Column<CargoProxy, String>(new ButtonCell()) {
            @Override
            public String getValue(CargoProxy object) {
                return "->";
            }
        };
        editCargoBtnCol.setFieldUpdater(new FieldUpdater<CargoProxy, String>() {

            @Override
            public void update(int index, CargoProxy object, String value) {
                presenter.goTo(new CargoDetailsPlace(String.valueOf(dataProvider.getList().get(index).getId())));
            }
        });
        return editCargoBtnCol;
    }
    public void setDataList(List<CargoProxy> dataList) {
        this.dataList = dataList;
        bindData(dataList);
    }

    public void bindData(List<CargoProxy> cargoes) {
        dataProvider.setList(cargoes);
        sortHandler.setList(dataProvider.getList());
        dataProvider.refresh();
    }
    private void filter(String filteredName) {
        List<CargoProxy> filteredCargoes = new ArrayList<>();
        for (CargoProxy cargo : dataList) {
            if (String.valueOf(cargo.getId()).contains(filteredName)) {
                filteredCargoes.add(cargo);
            }
        }

        bindData(filteredCargoes);
    }
}
